<template>
  <div class="login-container" :class="{ 'dark-mode': isDarkMode }">
    <div class="login-box">
      <!-- 登录表单区域 -->
      <div class="login-form-area">
        <div class="login-header">
          <img class="login-logo" src="../../assets/logo/logo.png" alt="Logo" />
          <h1 class="system-title">淘票票系统</h1>
        </div>
        <div class="welcome-text">欢迎使用，请登录</div>
        <a-form ref="formRef" :model="formData" layout="vertical" @submit="handleSubmit">
          <a-form-item field="username" :rules="[{ required: true, message: '请输入用户名' }]">
            <a-input v-model="formData.username" placeholder="用户名">
              <template #prefix>
                <icon-user />
              </template>
            </a-input>
          </a-form-item>
          <a-form-item field="password" :rules="[{ required: true, message: '请输入密码' }]">
            <a-input-password v-model="formData.password" placeholder="密码" allow-clear>
              <template #prefix>
                <icon-lock />
              </template>
            </a-input-password>
          </a-form-item>
          <div class="login-options">
            <a-checkbox v-model="formData.rememberMe">记住我</a-checkbox>
            <a-link>忘记密码？</a-link>
          </div>
          <a-form-item>
            <a-button type="primary" html-type="submit" long :loading="loading">
              登录
            </a-button>
          </a-form-item>
        </a-form>
        <div class="other-login">
          <div class="divider-text">其他登录方式</div>
          <div class="social-login">
            <a-button shape="circle" type="text">
              <template #icon><icon-wechat size="18" /></template>
            </a-button>
            <a-button shape="circle" type="text">
              <template #icon><icon-qq size="18" /></template>
            </a-button>
            <a-button shape="circle" type="text">
              <template #icon><icon-weibo size="18" /></template>
            </a-button>
          </div>
        </div>
      </div>
      
      <!-- 动画区域 -->
      <div class="animation-area">
        <div class="theme-switch">
          <a-button type="text" shape="circle" @click="toggleTheme">
            <template #icon>
              <icon-moon-fill v-if="isDarkMode" />
              <icon-sun-fill v-else />
            </template>
          </a-button>
        </div>
        <div class="animation-content">
          <div class="animation-title">观影体验，从这里开始</div>
          <div class="animation-svg">
            <!-- 动态SVG动画 -->
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 800 600" width="100%" height="100%">
              <defs>
                <linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
                  <stop offset="0%" stop-color="#4080FF" stop-opacity="0.8" />
                  <stop offset="100%" stop-color="#165DFF" stop-opacity="0.8" />
                </linearGradient>
              </defs>
              
              <!-- 背景元素 -->
              <rect width="800" height="600" fill="transparent" />
              
              <!-- 电影胶片 -->
              <g class="film-strip">
                <rect x="200" y="100" width="400" height="400" rx="10" fill="url(#grad1)" />
                
                <!-- 胶片孔 -->
                <circle cx="250" cy="150" r="15" fill="#fff" />
                <circle cx="250" cy="250" r="15" fill="#fff" />
                <circle cx="250" cy="350" r="15" fill="#fff" />
                <circle cx="250" cy="450" r="15" fill="#fff" />
                
                <circle cx="550" cy="150" r="15" fill="#fff" />
                <circle cx="550" cy="250" r="15" fill="#fff" />
                <circle cx="550" cy="350" r="15" fill="#fff" />
                <circle cx="550" cy="450" r="15" fill="#fff" />
                
                <!-- 电影屏幕 -->
                <rect x="280" y="180" width="240" height="160" fill="#FFF" />
                
                <!-- 电影票 -->
                <rect x="320" y="380" width="160" height="80" rx="5" fill="#FFF" />
                <line x1="400" y1="380" x2="400" y2="460" stroke="#4080FF" stroke-width="2" stroke-dasharray="5,5" />
                <circle cx="340" cy="400" r="10" fill="#4080FF" />
                <circle cx="340" cy="440" r="10" fill="#4080FF" />
                <rect x="370" y="395" width="60" height="10" fill="#4080FF" />
                <rect x="370" y="415" width="40" height="10" fill="#4080FF" />
                <rect x="370" y="435" width="50" height="10" fill="#4080FF" />
              </g>
              
              <!-- 装饰元素 - 星星 -->
              <g class="stars">
                <circle cx="150" cy="100" r="3" fill="#fff">
                  <animate attributeName="opacity" values="0;1;0" dur="3s" repeatCount="indefinite" />
                </circle>
                <circle cx="650" cy="120" r="2" fill="#fff">
                  <animate attributeName="opacity" values="0;1;0" dur="2s" repeatCount="indefinite" begin="1s" />
                </circle>
                <circle cx="700" cy="400" r="4" fill="#fff">
                  <animate attributeName="opacity" values="0;1;0" dur="4s" repeatCount="indefinite" />
                </circle>
                <circle cx="120" cy="350" r="3" fill="#fff">
                  <animate attributeName="opacity" values="0;1;0" dur="5s" repeatCount="indefinite" begin="0.5s" />
                </circle>
              </g>
              
              <!-- 装饰线条 -->
              <path d="M100,500 Q400,550 700,500" stroke="#4080FF" stroke-width="2" fill="transparent">
                <animate attributeName="d" dur="10s" repeatCount="indefinite"
                         values="M100,500 Q400,550 700,500;
                                M100,510 Q400,450 700,510;
                                M100,500 Q400,550 700,500" />
              </path>
            </svg>
          </div>
          <div class="animation-description">一站式电影购票管理平台</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import { useRouter } from 'vue-router';
import { Message } from '@arco-design/web-vue';
import { useUserStore } from '../../store/user';

const router = useRouter();
const userStore = useUserStore();
const formRef = ref(null);
const loading = ref(false);
const isDarkMode = ref(userStore.isDarkMode);

// 表单数据
const formData = reactive({
  username: '',
  password: '',
  rememberMe: false
});

// 登录提交
const handleSubmit = async () => {
  const { validate, getFieldsError } = formRef.value;
  
  try {
    loading.value = true;
    await validate();
    
    // 模拟登录验证
    if (formData.username === 'admin' && formData.password === '123456') {
      // 登录成功
      const token = 'mock-token-' + Math.random().toString(36).substring(2);
      const userInfo = {
        id: 1,
        username: formData.username,
        name: '管理员',
        avatar: '',
        roles: ['admin']
      };
      
      // 存储用户信息和token
      userStore.setToken(token);
      userStore.setUserInfo(userInfo);
      
      // 登录成功提示
      Message.success('登录成功，欢迎回来！');
      
      // 跳转到首页
      router.push('/');
    } else {
      // 登录失败
      Message.error('用户名或密码错误');
    }
  } catch (error) {
    console.error('登录验证失败:', error);
    const errors = getFieldsError();
    if (errors.length) {
      Message.error(errors[0].message);
    }
  } finally {
    loading.value = false;
  }
};

// 切换主题
const toggleTheme = () => {
  userStore.toggleTheme();
  isDarkMode.value = userStore.isDarkMode;
};
</script>

<style scoped>
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  width: 100%;
  background-color: var(--color-bg-1);
  overflow: hidden;
}

.login-box {
  display: flex;
  width: 900px;
  height: 600px;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  background-color: var(--color-bg-2);
}

.login-form-area {
  width: 400px;
  padding: 40px;
  position: relative;
  display: flex;
  flex-direction: column;
}

.animation-area {
  position: relative;
  flex: 1;
  background: linear-gradient(135deg, #4080FF 0%, #165DFF 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.login-header {
  display: flex;
  align-items: center;
  margin-bottom: 24px;
}

.login-logo {
  width: 48px;
  height: 48px;
  margin-right: 16px;
}

.system-title {
  font-size: 24px;
  font-weight: 600;
  color: var(--color-text-1);
  margin: 0;
}

.welcome-text {
  font-size: 16px;
  color: var(--color-text-2);
  margin-bottom: 40px;
}

.login-options {
  display: flex;
  justify-content: space-between;
  margin-bottom: 24px;
}

.other-login {
  margin-top: 40px;
}

.divider-text {
  text-align: center;
  color: var(--color-text-3);
  position: relative;
  margin: 24px 0;
}

.divider-text::before,
.divider-text::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 30%;
  height: 1px;
  background-color: var(--color-border-2);
}

.divider-text::before {
  left: 0;
}

.divider-text::after {
  right: 0;
}

.social-login {
  display: flex;
  justify-content: center;
  gap: 16px;
}

.animation-content {
  text-align: center;
  color: white;
  padding: 20px;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  z-index: 1;
}

.animation-title {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 30px;
}

.animation-svg {
  width: 100%;
  height: 400px;
  margin-bottom: 30px;
}

.animation-description {
  font-size: 16px;
  opacity: 0.9;
}

.theme-switch {
  position: absolute;
  top: 20px;
  right: 20px;
  z-index: 2;
}

/* 动画效果 */
.film-strip {
  animation: float 6s ease-in-out infinite;
}

@keyframes float {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-10px);
  }
}

.stars circle {
  animation: twinkle 4s ease-in-out infinite;
}

@keyframes twinkle {
  0%, 100% {
    opacity: 0.2;
  }
  50% {
    opacity: 1;
  }
}

/* 响应式 */
@media (max-width: 900px) {
  .login-box {
    width: 100%;
    height: 100%;
    border-radius: 0;
    flex-direction: column;
  }
  
  .login-form-area {
    width: 100%;
    order: 2;
  }
  
  .animation-area {
    height: 40%;
    order: 1;
  }
  
  .animation-svg {
    height: 200px;
  }
}

/* 暗黑模式 */
.dark-mode .login-form-area {
  background-color: var(--color-bg-2);
}
</style> 